## Create Figure 2

library(tidyverse)
library(data.table)

area = rep(c(5.753195, 8.855136), 50)
earmarks = rep(c(0 ,
                   0.1386294 ,
                   1.386294 ,
                   1.791759 ,
                   2.079442 ,
                   2.197225 ,
                   2.302585 ,
                   2.397895 ,
                   2.484907 ,
                   2.564949 ,
                   2.639057 ,
                   2.70805 ,
                   2.772589 ,
                   2.833213 ,
                   2.890372 ,
                   2.944439 ,
                   2.995732 ,
                   3.044522 ,
                   3.077086 ,
                   3.091042 ,
                   3.135494 ,
                   3.178054 ,
                   3.218876 ,
                   3.258097 ,
                   3.295837 ,
                   3.332205 ,
                   3.367296 ,
                   3.401197 ,
                   3.433987 ,
                   3.465736 ,
                   3.496508 ,
                   3.526361 ,
                   3.555348 ,
                   3.583519 ,
                   3.610918 ,
                   3.663562 ,
                   3.711103 ,
                   3.73767 ,
                   3.7612 ,
                   3.78419 ,
                   3.828641 ,
                   3.871201 ,
                   3.931826 ,
                   3.988984 ,
                   4.025352 ,
                   4.100903 ,
                   4.218026 ,
                   4.26268 ,
                   4.405359 ,
                   4.672829), each = 2)


## Load estimates and SEs
vote_dta = read.table("predictions.txt") %>%
  bind_cols(., area) %>%
  bind_cols(., earmarks) %>%
  rename(est = V2, se = V3,
         area = ...5, earmarks = ...6)

Figure2 = ggplot(vote_dta) +
  geom_ribbon(aes(x=earmarks, ymin=est - 1.96 * se, 
                  ymax=est + 1.96 * se, fill = factor(area)), alpha=0.4) +
  geom_line(aes(x=earmarks, y=est, color = factor(area))) +
  scale_color_manual(values = c("darkgreen", "darkblue"), name = "log(Area)",
                     labels = c("5.75", "8.86")) + 
  scale_fill_manual(values = c("darkgreen", "darkblue"), name = "log(Area)",
                    labels = c("5.75", "8.86")) +
  labs(x = "log(# of Earmarks)", y = "Predicted Incumbent Vote Share") +
  theme_bw() +
  theme(legend.position = "bottom",
        plot.margin = margin(10, 10, 10, 10))
ggsave(Figure2, file = "vote_share_predictions.png", 
       width = 4, height = 4, units = "in", dpi = 800)
